function recaulate_amount()
{
        if(isNaN(Math.round(parseFloat(document.getElementById("total_inc_new").value)*100)/100))
        {
                var new_amount = 0;
        }else{
                var new_amount = Math.round(parseFloat(document.getElementById("total_inc_new").value)*100)/100;
        }
        var old_amount = parseFloat(document.getElementById("total_ex_old").innerHTML);
        
        {% for tax in all_expense_taxes %}
                if(isNaN(Math.round(parseFloat(document.getElementById("{{tax['shortname']}}_total_tax_new").value)*100)/100))
                {
                        var tax_value = 0;
                }else{
                        var tax_value = Math.round(parseFloat(document.getElementById("{{tax['shortname']}}_total_tax_new").value)*100)/100;
                }
                new_amount -= tax_value;
        {% endfor %}
        
        var d_value = Math.round((new_amount-old_amount)*100)/100;
        
        if(d_value != 0 && !isNaN(d_value))
        {
                document.getElementById("amount_changed").style.display="block";
                document.getElementById("amount_changed_message").innerHTML = "You have changed amount exclude tax into " + d_value.toString();
                document.getElementById("total_ex_new").innerHTML =  (Math.round(new_amount*100)/100).toString();
        }
        else
        {
                document.getElementById("amount_changed").style.display="none";
                document.getElementById("amount_changed_message").innerHTML = "";
                document.getElementById("comment_for_change").value = "";
                document.getElementById("expense_account").value = "";
                document.getElementById("total_ex_new").innerHTML =  (Math.round(new_amount*100)/100).toString();
        }
}

function sum_amount()
{
        var total_ex = 0;
        var total_inc = 0;
        {% for tax in all_expense_taxes %}
                var total_tax_{{tax['shortname']}} = 0;
        {% endfor %}
        
        var purchase_order_list = document.getElementsByName("purchase_order_ids");
        for(i=0;i<purchase_order_list.length;i++)
        {
                if(purchase_order_list[i].checked == true)
                {
                        total_ex += Math.round(parseFloat(document.getElementById('purchase_'+purchase_order_list[i].value.toString()+"_ex").value)*100)/100;
                        total_inc += Math.round(parseFloat(document.getElementById('purchase_'+purchase_order_list[i].value.toString()+"_inc").value)*100)/100;
                        {% for tax in all_expense_taxes %}
                                if(document.getElementById('purchase_'+purchase_order_list[i].value.toString()+"_{{tax['shortname']}}"))
                                {
                                        total_tax_{{tax['shortname']}} += Math.round(parseFloat(document.getElementById('purchase_'+purchase_order_list[i].value.toString()+"_{{tax['shortname']}}").value)*100)/100;
                                }
                        {% endfor %}
                }
        }
        document.getElementById("total_ex_old").innerHTML =  (Math.round(total_ex*100)/100).toString();
        document.getElementById("total_ex_new").innerHTML =  (Math.round(total_ex*100)/100).toString();
        document.getElementById("total_inc_old").innerHTML =  (Math.round(total_inc*100)/100).toString();
        document.getElementById("total_inc_new").value =  (Math.round(total_inc*100)/100).toString();
        {% for tax in all_expense_taxes %}
                document.getElementById("{{tax['shortname']}}_total_tax_old").innerHTML =  (Math.round(total_tax_{{tax['shortname']}}*100)/100).toString();
                document.getElementById("{{tax['shortname']}}_total_tax_new").value =  (Math.round(total_tax_{{tax['shortname']}}*100)/100).toString();
        {% endfor %}
}
function show_purchase_order_list()
{
		var supplier_id_selected = document.getElementById("supplier").value;
		{% for po in purchase_order_list %}
				var purchase_order = document.getElementById("supp_{{po['id']}}_{{po['supplier_id']}}")
				//var supplier_list = document.getElementsByName("{{su['id']}}");
				//if(navigator.userAgent.indexOf("MSIE")>=0||navigator.userAgent.indexOf("Opera")>=0){
				//		supplier_list = getElementsByName_ie('dl',"{{su['id']}}");						
				//}
				//for(i=0;i<supplier_list.length;i++)
				//{
				//		supplier_list[i].style.display="none";
				//}				
				if(document.getElementById("purchase_{{po['supplier_id']}}_{{po['id']}}")!=null)
				{
						document.getElementById("purchase_{{po['supplier_id']}}_{{po['id']}}").checked = false;
				}
				purchase_order.style.display='none';
		{% endfor %}
		document.getElementById("checkall").checked = false;
		document.getElementById("check_all").style.display = "none";
		if(supplier_id_selected != "")
		{
				{% for po in purchase_order_list %}
					if({{po['supplier_id']}}==parseInt(supplier_id_selected)){
							var purchase_order=document.getElementById("supp_{{po['id']}}_{{po['supplier_id']}}");
							purchase_order.style.display="";
					}
				{% endfor %}
				//var supplier_list = document.getElementsByName(supplier_id_selected);
				//if(navigator.userAgent.indexOf("MSIE")>=0||navigator.userAgent.indexOf("Opera")>=0){
				//		supplier_list = getElementsByName_ie('dl',supplier_id_selected);						
				//}
				//for(i=0;i<supplier_list.length;i++)
				//{
				//		supplier_list[i].style.display="";
				//}
				document.getElementById("check_all").style.display = "";
		}
		sum_amount();
}
function check_all()
{
		var supplier_id_selected = document.getElementById("supplier").value;
		if(supplier_id_selected != ""){
				{% for po in purchase_order_list %}
						if(document.getElementById('purchase_'+supplier_id_selected+"_{{po['id']}}")!=null)
						{
								document.getElementById('purchase_'+supplier_id_selected+"_{{po['id']}}").checked = document.getElementById("checkall").checked;
						}
				{% endfor %}
		}
		{% for su in suppliers %}
				//var supplier_list = document.getElementsByName("{{su['id']}}");
				//if(navigator.userAgent.indexOf("MSIE")>=0||navigator.userAgent.indexOf("Opera")>=0){
				//		supplier_list = getElementsByName_ie('dl',"{{su['id']}}");						
				//}
				//if(supplier_list[0] && supplier_list[0].style.display != "none")
				//{
				//		{% for po in purchase_order_list %}
				//				if(document.getElementById("{{su['id']}}_{{po['id']}}")!=null)
				//				{
				//						document.getElementById("{{su['id']}}_{{po['id']}}").checked = document.getElementById("checkall").checked;
				//				}
				//		{% endfor %}
				//}
		{% endfor %}
		sum_amount();
}
